// var {
//   http
// } = require('../../utils/apiutils');
let ctx = null; //Camera
// const qiniu = require("../../utils/qnutils");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    baseImg: "",
    isSuccess: false,
    type: '',  // 打开拍照 判断上班下班字段  sbsj上班  xbsj下班
    info: {

    },
    position:'front'
  },
  clearPhoto: function () {
    this.setData({
      baseImg: "",
      isSuccess: false,
    })
  },
  confirmBack () {
    let page = getCurrentPages()
    let prevPage = page[page.length - 2];
    var arr = prevPage.data.imgList;
    arr.push(this.data.baseImg)
    prevPage.setData({
      imgList: arr,
    })
    wx.navigateBack({
      delta: 1,
    })
  },
  getUpToken: function (url) {
    var that = this;
    http.getUpToken({},
      function (res) {
        // console.log("----七牛云的授权码---", res);
        qiniu.initQiniu(res.data);
        that.upDataImage(url);
      },
      function (err) {

      })
  },
  upDataImage: function (tempFilePaths) {
    var that = this;
    qiniu.getUploadImgae(tempFilePaths,
      function (res) {
        console.log("--------上传人脸认证数据--", res);
        var data = {
          url: res.key,
          trainingId: that.data.trainingId,
          groupId: that.data.groupId,
        }
        that.faceAuth(data)
      },
      function (err) { })
  },
  faceAuth: function (data) {
    var that = this;
    console.log("-----头像链接---------", data);
    http.faceAuth(data,
      function (res) {
        if (res.status == 200) {
          wx.showToast({
            title: '认证成功',
            icon: "none",
          })
          that.pushVodeo();
        } else {
          wx.showToast({
            title: res.message,
            icon: "none",
          })
          that.clearPhoto();
        }
      },
      function (err) {
      })
  },

  pushVodeo: function () {
    var pages = getCurrentPages();
    var prevPage = pages[pages.length - 2];  //上一个页面
    prevPage.setData({
      isRenZ: true
    })
    wx.navigateBack({
      delta: 1,
    })

  },
  takePhoto() {
    console.log("--------生成照片-------")
    ctx = wx.createCameraContext(); //初始化camera
    ctx.takePhoto({ //生成图片
      quality: 'high',
      success: (res) => { //得到图片
        this.setData({
          baseImg: res.tempImagePath,
          isSuccess: true,
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    let { trainingId, groupId } = options
    this.data.trainingId = trainingId;
    this.data.groupId = groupId;
    if (options.type) {
      this.data.type = options.type
    }
    wx.getSystemInfo({
      success: res => {
        this.setData({
          info: res,
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var shareObj = {
      title: "分享岗位", // 默认是小程序的名称(可以写slogan等)
      path: getApp().globalData.userInfo.inviteCode?'/pages/index/index?code='+getApp().globalData.userInfo.inviteCode:'/pages/index/index', // 
      imageUrl: '',
      success: function (res) {
        // 转发成功之后的回调
        if (res.errMsg == 'shareAppMessage:ok') {
        }
      }
    }
    console.log("------------", shareObj);
    // 返回shareObj
    return shareObj;
  },
  changepz(){
    if(this.data.position=='back'){
      this.setData({
        position:'front'
      })
      return;
    }
    this.setData({
      position:'back'
    })
  }
})